<template>
  <div>
    <el-form ref="form" :model="formData" label-width="70px" size="small">
      <el-row :gutter="24">
        <el-col :xs="24" :sm="24" :md="16" :lg="16" :xl="16">
          <el-form-item :label="$t('run.phase_desc')">
            <el-input type="textarea" disabled :rows="3" placeholder="" v-model="formData.textarea"></el-input>
          </el-form-item>
          <el-button type="primary" :disabled="(stepObj.isActive != 1 || phaseIsdis) || isStep || isoperation == 1 || isUnitConfirm"
            :loading="loading" size="small" style="float:right;margin-bottom:18px"
            @click="handleConfirm(40)">{{ $t("run.confirm") }}</el-button>
        </el-col>

        <el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="8">
          <div class="tableAuto">
            <el-table border v-loading="tableLoading" :data="tableData" style="width: 100%" :header-cell-style="{background:'#EAEBEC'}">
              <el-table-column prop="phaseName" :label="$t('basic.stage_name')" align="center"></el-table-column>
              <!-- <el-table-column prop="mode" label="模式" align="center"></el-table-column> -->
              <el-table-column prop="state" :label="$t('recipe.state')" align="center"></el-table-column>
            </el-table>
          </div>
          <div class="btnfang" style="text-align:center;margin-top:18px">
            <!-- phaseCode  TEM  温度控制暂时把暂停 启动 退出 禁止掉 -->
            <el-button :disabled="(stepObj.isActive != 1 || button1 || phaseIsdis) || isStep || isoperation == 1" type="primary"
              size="small" @click="handleEditPhaseStatus(10,'启动')">{{ $t("run.start_up") }}</el-button>
            <el-button v-if="isInfoPhase"
              :disabled="stepObj.isActive != 1 || button2 || phaseIsdis || isStep || isoperation == 1 || stepObj.phaseCode == 'PH_TEM'"
              size="small" @click="handleEditPhaseStatus(20,'暂停')">{{ $t("run.suspend") }}</el-button>
            <el-button v-if="isInfoPhase"
              :disabled="stepObj.isActive != 1 || button3 || phaseIsdis || isStep || isoperation == 1 || stepObj.phaseCode == 'PH_TEM'"
              size="small" @click="handleEditPhaseStatus(21,'重启')">{{ $t("run.restart") }}</el-button>
            <el-button v-if="isInfoPhase"
              :disabled="stepObj.isActive != 1 || button3 || phaseIsdis || isStep || isoperation == 1 || stepObj.phaseCode == 'PH_TEM'"
              size="small" @click="handleEditPhaseStatus(50,'退出')">{{ $t("run.quit") }}</el-button>
            <el-button v-if="isSHowstopBtn" :disabled="stepObj.isActive != 1 || button4 || phaseIsdis || isStep || isoperation == 1"
              size="small" @click="handleEditPhaseStatus(40,'完成')">{{ $t("workorder.finish") }}</el-button>
          </div>

        </el-col>
      </el-row>
    </el-form>

  </div>
</template>

<script>
import { formula } from "@/api/formula"
import { plc } from "@/api/plc"
export default {
  props: {
    stepObj: Object,
    count: Number,
    unitStat: Number,
    phaseIsdis: Boolean,
    formsSeqNo: [String, Number],
    isoperation: [Number, Object],
    idObj: Object,
    woId: String,
  },
  data () {
    return {
      tableData: [{
        phaseName: "",
        mode: '',
        state: ''
      }],
      formData: {
        textarea: ""
      },
      tableLoading: false,
      loading: false
    }
  },
  watch: {
    count (val) {
      if (val) {
        console.log(this.stepObj)
        this.tableData = [{
          phaseName: this.stepObj.phaseDesc,
          mode: this.stepObj.isHand == 1 ? "手动" : this.stepObj.isHand == 0 ? "自动" : '',
          state: this.queryPhase(this.unitStat)
        }]

        this.formData.textarea = this.stepObj.guideInfo
      }
    }
  },
  computed: {
    isStep () {
      if (parseInt(this.stepObj.seqNo || 0) == this.formsSeqNo) {
        return false
      } else {
        return true
      }
    },
    isInfoPhase () {
      if (parseInt(this.stepObj.serialNo) === 0 || this.stepObj.serialNo == 7 || this.stepObj.serialNo == 8 || this.stepObj.serialNo == 4 || this.stepObj.serialNo == 6 || this.stepObj.serialNo == 9 || this.stepObj.serialNo == 10) {
        return false
      } else {
        return true
      }
    },
    isSHowstopBtn () {
      if (parseInt(this.stepObj.serialNo) === 0 || this.stepObj.serialNo == 7 || this.stepObj.serialNo == 8 || this.stepObj.serialNo == 4 || this.stepObj.serialNo == 5 || this.stepObj.serialNo == 6 || this.stepObj.serialNo == 9 || this.stepObj.serialNo == 10) {
        return true
      } else {
        return false
      }
    },
    // 6温度控制   9蒸馏-氮气置换  10 蒸馏-冷凝接收
    button1 () { //启动按钮
      let arr = ['1', '2', '3', '5', '11', '12']
      let arr1 = ['0', '7', '8', '4', '6', '9', '10']
      if (this.unitStat === 0 || this.unitStat == 11) {
        if (arr.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else if (arr1.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else if (this.unitStat == 51) {
        let arr2 = ['5', '1', '2', '3', '11', '12']
        if (arr2.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    },
    button2 () { //暂停按钮
      let arr = ['5', '1', '2', '3', '11', '12']
      if (this.unitStat == 10) {
        if (arr.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    },
    button3 () { //重启 终止
      let arr = ['5', '1', '2', '3', '11', '12']
      if (this.unitStat == 21) {
        if (arr.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    },
    button4 () { //停止完成
      let arr = ['5']
      let arr1 = ['0', '7', '8', '4', '6', '9', '10']
      if (this.unitStat == 51) {
        if (arr.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else if (this.unitStat == 10) {
        if (arr1.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }

      } else if (this.unitStat == 21) {
        if (arr.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    },
    isUnitConfirm () {
      let arr = [0, 4, 7, 8]
      if (arr.indexOf(parseInt(this.stepObj.serialNo)) > -1) {
        return false
      } else {
        return true
      }
    }
  },
  created () {
    //信息提示 ifo     物料产出 PH_OTM
  },
  methods: {
    handleConfirm (type) {
      if (!this.stepObj.fid) {
        this.$message.error('请选择阶段');
        return
      }
      let that = this
      const param = {
        fid: this.stepObj.fid,
        stateCd: type
      }
      plc.sendPlcCommandPhase(param).then(res => {
        if (res.code == 200) {
          if (that.stepObj.serialNo == 7) {
            that.submitDescribeCommand()
          }
          that.$message({ duration: 1000, type: 'success', message: '确认成功!' });
        }
      })
    },
    handleEditPhaseStatus (type, txt) {
      if (!this.stepObj.fid) {
        this.$message.error('请选择阶段');
        return
      }
      let that = this
      this.$customConfirm({
        content: '是否要' + txt + '此阶段',
        onok () {
          const param = {
            fid: that.stepObj.fid,
            stateCd: type
          }
          plc.sendPlcCommandPhase(param).then(res => {
            if (res.code == 200) {
              that.$message({ duration: 1000, type: 'success', message: txt + '成功!' });
              if (type == 40 && that.stepObj.serialNo == 7) {
                that.submitDescribeCommand()
              }

            }
          })
        },
      })
    },
    //阶段描述
    submitDescribeCommand () {
      const param = {
        type: 2,
        flastModifyComment: this.formData.textarea,
        level: 3,
        unitId: this.idObj.equipmentId,
        woId: this.woId,
        phaseId: this.stepObj.fid,
        jobId: this.idObj.jobId
      }
      plc.submitDescribeCommand(param).then(res => {

      })
    },
    queryPhase (state) {
      if (state == 10) {
        return this.$t('workorder.function')
      } else if (state == 11) {
        return this.$t('workorder.finish')
      } else if (state == 21) {
        return this.$t('workorder.suspend')
      } else if (state == 41) {
        return "结束"
      } else if (state == 51) {
        return "异常结束"
      } else if (state == 0) {
        return "准备"
      } else {
        return ""
      }
    }
  }
}
</script>
<style scoped>
.btnfang {
  text-align: center;
}
.btnfang >>> .el-button {
  border-radius: 0;
  padding: 16px 12px;
}
.tableAuto >>> .cell {
  min-height: 23px;
}
</style>